import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
   {
    //管理后台主界面
    path:'/',
    component:()=>import('../views/main/main.vue'),
    children:[
      {path:'menus',component:()=>import('../views/menu/menu.vue')},
      {path:'roles',component:()=>import('../views/role/role.vue')},
      //管理员管理
      {
        path:'manage',
        meta:{title:'管理员管理'},
        component:()=>import('../views/manage/manage.vue')
      },
      //商品分类管理
      {
        path:'category',
        meta:{title:'商品分类'},
        component:()=>import('../views/category/category.vue')
      },
      //商品规格管理
      {
        path:'specs',
        meta:{title:'商品规格'},
        component:()=>import('../views/specs/specs.vue')
      },  
      //商品管理
      {
        path:'goods',
        meta:{title:'商品管理'},
        component:()=>import('../views/goods/goods.vue')
      },           
    ]
  },  
   {path:'/denglu',component:()=>import('../views/login/login.vue')},  
]

const router = new VueRouter({
  routes
})

//全局前置守卫：
router.beforeEach((to,from,next)=>{

  //访问登录界面：
  if(to.path == '/denglu'){ 
     next();
     return;
  }

  //获取已登录用户所分配的菜单：
  let userMenuArr =  sessionStorage['manaInfos']?JSON.parse(sessionStorage['manaInfos']).menus_url:[];
  let menuArr = ['/','/denglu',...userMenuArr]
  
  if(menuArr.indexOf(to.path) == -1){ //用户要访问的菜单路径不被允许：
     next('/denglu');
    return;
  }else{  //用户要访问的菜单路径允许访问：
    next();
  }
  
});

export default router
